package com.ncst.backend.controller;

import com.ncst.backend.common.Result;
import com.ncst.backend.domain.DrugLibrary;
import com.ncst.backend.service.DrugLibraryService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/druglib")
@RequiredArgsConstructor
public class DrugLibraryController {

    private final DrugLibraryService drugService;

    /** 搜索 + 列表（前端分页，后端全量） */
    @GetMapping("/list")
    public Result<List<DrugLibrary>> list(@RequestParam(required = false) String key,
                                          @RequestParam(required = false) String category) {
        return Result.success(drugService.search(key, category));
    }

    /** 新增药品 */
    @PostMapping
    public Result<Void> add(@RequestBody DrugLibrary drug) {
        return drugService.save(drug) ? Result.success("新增成功") : Result.error("新增失败");
    }

    /** 修改药品 */
    @PutMapping("/{id}")
    public Result<Void> update(@PathVariable Integer id, @RequestBody DrugLibrary drug) {
        drug.setId(id);
        return drugService.updateById(drug) ? Result.success("修改成功") : Result.error("修改失败");
    }

    /** 删除药品 */
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Integer id) {
        return drugService.removeById(id) ? Result.success("删除成功") : Result.error("删除失败");
    }
}